package com.seas.web;

import com.seas.dao.PeopleRepository;
import com.seas.dao.PersonRepository;
import com.seas.dao.StudentDao;
import com.seas.domain.Location;
import com.seas.domain.People;
import com.seas.domain.Person;
import com.seas.domain.Student;
import com.seas.service.CacheService;
import com.seas.service.TransactionDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * Created by Seas Cheng on 2017/9/25
 */
@RestController
public class DataController {
    @Autowired
    PersonRepository personRepository;

    @Autowired
    TransactionDemoService transactionDemoService;

    @Autowired
    CacheService cacheService;

    @Autowired
    PeopleRepository peopleRepository;

    @Autowired
    StudentDao studentDao;

    @RequestMapping("/save")
    public Person save(String name, String address, Integer age){
        Person p = personRepository.save(new Person(null, name, age, address));
        return p;
    }

    @RequestMapping("/q1")
    public List<Person> q1(String address){
        List<Person> personList = personRepository.findByAddress(address);
        return personList;
    }

    //排序
    @RequestMapping("/sort")
    public List<Person> sort(){
        List<Person> people = personRepository.findAll(new Sort(Sort.Direction.ASC, "age"));
        return people;
    }

    //分页
    @RequestMapping("/page")
    public Page<Person> page(){
        Page<Person> personPage = personRepository.findAll(new PageRequest(2, 2));
        return personPage;
    }

    //数量
    @RequestMapping("/count")
    public Long count(){
        Long personCount = personRepository.count();
        return personCount;
    }

    //排序
    @RequestMapping("/auto")
    public List<Person> auto(){
        Person person = new Person();
        person.setName("seas");
        List<Person> personList = personRepository.findBySortAuto(person, new Sort(Sort.Direction.ASC, "age"));
        return personList;
    }

    //事务回滚
    @RequestMapping("/rollback")
    public Person rollback(){
        Person person = new Person();
        person.setName("cheng");
        return transactionDemoService.savePersonWithRollBack(person);
    }

    //事务不回滚
    @RequestMapping("/norollback")
    public Person noRollback(){
        Person person = new Person();
        person.setName("cheng");
        return transactionDemoService.savePersonWithoutRollback(person);
    }

    //缓存，保存并存入
    @RequestMapping("/cache_put")
    public Person cachePut(){
        Person person = new Person();
        person.setName("Cathy");
        person.setAge(28);
        person.setAddress("桃园");
        return cacheService.save(person);
    }

    //缓存，从缓存读取
    @RequestMapping("cacheable")
    public Person cacheCacheable(Person person){
        Person p = new Person();
        p.setId((long) 11);
        return cacheService.findOne(p);
    }

    //缓存，删除缓存
    @RequestMapping("/cacheevit")
    public String cacheEvit(Long id){
        cacheService.remove((long)11);
        return "OK";
    }

    //mongodb 保存
    @RequestMapping("/mongodbSave")
    public People mongodbSave(){
        People p = new People("wyf", 32);
        Collection<Location> locations = new LinkedHashSet<Location>();
        Location loc1 = new Location("潍坊", "2005");
        Location loc2 = new Location("黄岛", "2005");
        Location loc3 = new Location("青岛","2012");
        locations.add(loc1);
        locations.add(loc2);
        locations.add(loc3);
        p.setLocations(locations);
        return peopleRepository.save(p);
    }

    //mongodb 查询
    @RequestMapping("mongodbQuery")
    public People mongodbQuery(){
        return peopleRepository.findByName("wyf");
    }

    //mongodb 自定义查询
    @RequestMapping("mongodbQuery2")
    public List<People> mongodbQuery2(){
        return peopleRepository.withQueryFindByAge(32);
    }

    //Redis set
    @RequestMapping("redisSet")
    public void redisSet(){
        Student student = new Student("1","seas",30);
        studentDao.save(student);
        studentDao.stringRedisTemplateDemo();
    }

    @RequestMapping("redisStr")
    public String getRedisStr(){
        return studentDao.getString();
    }

    @RequestMapping("redisStudent")
    public Student getRedisStudent(){
        return studentDao.getStudent();
    }

}
